package cn.edu.scau.dormitory_management_system.controller;

import java.util.*;

import cn.edu.scau.dormitory_management_system.entity.BillEntity;
import cn.edu.scau.dormitory_management_system.service.BillService;
import cn.edu.scau.dormitory_management_system.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("/admin/bill")
public class BillController {


    @Autowired
    BillService billService;

    @PostMapping("create")
    public JSONObject create(@RequestBody BillEntity billEntity){
        boolean flag = billService.create(billEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }
    
    @PostMapping("getDormNames")
    public JSONObject get(int dormBuildId) {
        JSONObject object = new JSONObject();

        QueryWrapper<BillEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dorm_build_id", dormBuildId);
        List<String> dormNames = new ArrayList<>();
        for (BillEntity billEntity : billService.list(wrapper)) {
            if(billEntity.getDormName().isEmpty()) continue;
            if(!dormNames.contains(billEntity.getDormName()))
                dormNames.add(billEntity.getDormName());
        }

        if(dormNames.isEmpty()){
            object.put("code", 0);
            object.put("msg", "无数据");
        }else{
            Collections.sort(dormNames);
            object.put("code", 200);
            object.put("dormNames", dormNames);
        }

        return object;
    }

    @PostMapping("delete")
    public JSONObject delete(@RequestBody BillEntity billEntity){
        boolean flag = billService.delete(billEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("update")
    public JSONObject update(@RequestBody BillEntity billEntity){
        boolean flag = billService.update(billEntity);
        JSONObject object = new JSONObject();
        if(flag){
            object.put("code", 200);
            object.put("msg", "操作成功");
        }else{
            object.put("code", 0);
            object.put("msg", "操作失败");
        }
        return object;
    }

    @PostMapping("getAllBuildId")
    public JSONObject getAllBuildId(){
        JSONObject object = new JSONObject();
        List<BillEntity> billEntities = billService.list();
        Set<Integer> buildId = new HashSet<>();
        for (BillEntity billEntity:billEntities
             ) {
            buildId.add(billEntity.getDormBuildId());
        }
        if (buildId.isEmpty()){
            object.put("code",0);
            object.put("msg","无数据");
        }else {
            object.put("code",200);
            object.put("buildId",buildId);
        }
        return object;
    }

    @PostMapping("getAllDormName")
    public JSONObject getAllDormName(){
        JSONObject object = new JSONObject();
        List<BillEntity> billEntities = billService.list();
        Set<String> DormName = new HashSet<>();
        for (BillEntity billEntity:billEntities
        ) {
            DormName.add(billEntity.getDormName());
        }
        if (DormName.isEmpty()){
            object.put("code",0);
            object.put("msg","无数据");
        }else {
            object.put("code",200);
            object.put("buildId",DormName);
        }
        return object;
    }

    @PostMapping("findByDormName")
    public JSONObject findByDormName(@RequestParam("dormName") String dormName,
                                     @RequestParam(defaultValue = "1") int current,
                                     @RequestParam(defaultValue = "8") int size){
        JSONObject object = new JSONObject();
        double sum = 0;
        IPage<BillEntity> page = new Page<>(current,size);
        QueryWrapper<BillEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("bill_id");
        if(StringUtils.isEmpty(dormName)) {
            page = billService.page(page, wrapper);
        }else {
            wrapper.like("dorm_name", dormName).or().eq("dorm_build_id", dormName);
            page = billService.page(page, wrapper);
        }
        /*
        object.put("bills",billEntities);
        */
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

    @PostMapping("findByBuildId")
    public JSONObject findByBuildId(@RequestParam("dormBuildId") int dormBuildId,
                                    @RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "8") int size){
        JSONObject object = new JSONObject();
        double sum = 0,water=0,energy=0;
        IPage<BillEntity> page = new Page<>(current,size);
        QueryWrapper<BillEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dorm_build_id", dormBuildId);
        wrapper.orderByDesc("bill_time");
        page = billService.page(page,wrapper);
        List<BillEntity> billEntities = billService.findByDormBuildId(dormBuildId);
        for (BillEntity billEntity: billEntities
        ) {
            sum+=billEntity.getSurplus();
            water+=billEntity.getWater();
            energy+=billEntity.getEnergy();
        }
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        object.put("water",water);
        object.put("energy",energy);
        object.put("sum",sum);
        return object;
    }

    @PostMapping("getAllBill")
    public JSONObject getAllBill(@RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "8") int size){
        JSONObject object = new JSONObject();
        QueryWrapper<BillEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("bill_id");
        IPage<BillEntity> page = new Page<>(current,size);
        page = billService.page(page,queryWrapper);
        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }
        return object;
    }

}
